package com.eleven.mall.service.impl;

import com.eleven.mall.mapper.Reports1Mapper;
import com.eleven.mall.pojo.*;
import com.eleven.mall.pojo.entity.Reports1;
import com.eleven.mall.pojo.entity.Reports1Example;
import com.eleven.mall.service.DataService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: Rui_simple
 * @Date: 2022/10/25 23:31
 * @Description: DataServiceImpl
 * @Version 1.0.0
 */
@Service
@AllArgsConstructor
public class DataServiceImpl implements DataService {

    Reports1Mapper reports1Mapper;

    @Override
    public DataOut getEChart() {
        DataOut dataOut = new DataOut();
        Lengend lengend = new Lengend();

        //------lengend
        List<String> date = reports1Mapper.selectArea();
        lengend.setData(date);
        dataOut.setLegend(lengend);

        //------xAxis
        Reports1Example example = new Reports1Example();
        List<Reports1> reports1s = reports1Mapper.selectByExample(example);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        ArrayList<String> dataList = new ArrayList<>();
        for (Reports1 r : reports1s) {
            dataList.add(df.format(r.getRp1Date()));
            //System.out.println(r);
        }
        List<String> dataList2 = dataList.stream().distinct().collect(Collectors.toList());

        XAxis xaxis = new XAxis();
        xaxis.setData(dataList2);
        ArrayList<XAxis> xAxisList = new ArrayList<>();
        xAxisList.add(xaxis);
        dataOut.setXAxis(xAxisList);

        //-----yAxis
        YAxis yAxis = new YAxis();
        yAxis.setType("value");
        ArrayList<YAxis> yAxisList = new ArrayList<>();
        yAxisList.add(yAxis);
        dataOut.setYAxis(yAxisList);

        //-----series
        ArrayList<Series> seriesArrayList = new ArrayList<>();
        for (String d : date) {
            Series series = new Series();
            series.setName(d);
            series.setType("line");
            series.setStack("总量");
            //series.setAreaStyle(null);
            List<Integer> data = reports1Mapper.selectUserCountByArea(d);
            series.setData(data);
            seriesArrayList.add(series);
        }
        dataOut.setSeries(seriesArrayList);

        return dataOut;
    }
}
